﻿using System;
using System.Collections.Generic;
using System.Text;

namespace dotHTML.HTML {
    /// <summary>
    /// Defines an HTML button element, as used in a form.
    /// </summary>
    [Serializable]
    public class Button : Element {

        /// <summary>
        /// Determines the action of this button.
        /// </summary>
        public enum ButtonType {
            /// <summary>
            /// Corresponds to a form submit button.
            /// </summary>
            Submit,

            /// <summary>
            /// Corresponds to a form reset button.
            /// </summary>
            Reset,

            /// <summary>
            /// Corresponds to a generic form button.
            /// </summary>
            Button
        }

        private bool _autofocus;
        private bool _disabled;
        private Form _form;
        private string _formaction;
        private string _formenctype;
        private FormMethodChoices? _formmethod;
        private bool _formnovalidate;
        private string _formtarget;
        private string _name;
        private ButtonType? _type;
        private string _value;

        /// <summary>
        /// Determines whether or not this button has input focus when the page
        /// loads.
        /// </summary>
        public bool AutoFocus {
            get { return _autofocus; }
            set { _autofocus = value; }
        }

        /// <summary>
        /// Determines whether or not this button is disabled.
        /// </summary>
        public bool Disabled {
            get { return _disabled; }
            set { _disabled = value; }
        }

        /// <summary>
        /// Determines which form this button is a child of.
        /// </summary>
        public Form Form {
            get { return _form; }
            set { _form = value; }
        }

        /// <summary>
        /// Determines the URL to submit the form to.
        /// </summary>
        public string FormAction {
            get { return _formaction; }
            set { _formaction = value; }
        }

        /// <summary>
        /// Determines the MIME type used to submit the form.
        /// </summary>
        public string FormEncType {
            get { return _formenctype; }
            set { _formenctype = value; }
        }

        /// <summary>
        /// Determines the HTTP method used to submit the form.
        /// </summary>
        public FormMethodChoices? FormMethod {
            get { return _formmethod; }
            set { _formmethod = value; }
        }

        /// <summary>
        /// Determines whether or not to validate the form on submission.
        /// </summary>
        public bool FormNoValidate {
            get { return _formnovalidate; }
            set { _formnovalidate = value; }
        }

        /// <summary>
        /// Determines the target for the response to the form submission.
        /// </summary>
        public string FormTarget {
            get { return _formtarget; }
            set { _formtarget = value; }
        }

        /// <summary>
        /// Determines the name of this button.
        /// </summary>
        public string Name {
            get { return _name; }
            set { _name = value; }
        }

        /// <summary>
        /// Determines the type of this button.
        /// </summary>
        public ButtonType? Type {
            get { return _type; }
            set { _type = value; }
        }

        /// <summary>
        /// Determines the value of this button.
        /// </summary>
        public string Value {
            get { return _value; }
            set { _value = value; }
        }

        /// <summary>
        /// Gets a formatted string of all local attributes of this element
        /// (not including the base attributes).
        /// </summary>
        /// <param name="sb">The StringBuilder to write this element's
        /// attribute data to.</param>
        protected override void WriteLocalAttributes(StringBuilder sb) {
            AttributeWriter.Write(sb, _autofocus, "autofocus");
            AttributeWriter.Write(sb, _disabled, "disabled");
            if (_form != null) {
                AttributeWriter.Write(sb, _form.ID, "form");
            }
            AttributeWriter.Write(sb, _formaction, "formaction");
            AttributeWriter.Write(sb, _formenctype, "formenctype");
            AttributeWriter.Write(sb, _formmethod, "formmethod", true);
            AttributeWriter.Write(sb, _formnovalidate, "formnovalidate");
            AttributeWriter.Write(sb, _formtarget, "formtarget");
            AttributeWriter.Write(sb, _name, "name");
            AttributeWriter.Write(sb, _type, "type", true);
            AttributeWriter.Write(sb, _value, "value");
        }
    }
}
